#include "RunConfig.h"

RunConfig::RunConfig(const std::string& filePath)
{
    bool ret;
    ret = loadConfig(filePath);
    if (ret == true){
        std::cout << "读取"<< filePath <<"文件，成功！\n";

    }else{
        std::cout << "读取"<< filePath <<"文件，失败~~~\n";
    }
}

RunConfig::~RunConfig()
{
}

// 加载配置文件
bool RunConfig::loadConfig(const std::string& filePath) {
    std::ifstream file(filePath);
    if (!file.is_open()) {
        std::cerr << "Failed to open config file: " << filePath << std::endl;
        return false;
    }

    std::string jsonString;
    std::string line;
    while (std::getline(file, line)) {
        jsonString += line;
    }

    try {
        configJson = nlohmann::json::parse(jsonString);
    } catch (const nlohmann::json::parse_error& e) {
        std::cerr << "Error parsing JSON: " << e.what() << std::endl;
        return false;
    }

    file.close();
    return true;
}


std::string RunConfig::getString(const std::string& key, const std::string& defaultValue) const {
    return configJson.value(key, defaultValue);
}

QString RunConfig::getQString(const std::string& key, const std::string& defaultValue) const{
    return QString::fromStdString(configJson.value(key, defaultValue));
}

int RunConfig::getInt(const std::string& key, int defaultValue) const {
    return configJson.value(key, defaultValue);
}

bool RunConfig::getBool(const std::string& key, bool defaultValue) const {
    return configJson.value(key, defaultValue);
}

double RunConfig::getDouble(const std::string& key, double defaultValue) const {
    return configJson.value(key, defaultValue);
}

void RunConfig::printfConfig(void){
    std::cout << configJson.dump(4) << std::endl;
}
